package parse;

import org.apache.commons.digester.Digester;
import org.apache.commons.digester.xmlrules.DigesterLoader;
import org.xml.sax.SAXException;

import java.io.IOException;

public class DigesterParse {

    public static void main(String[] args) {
        // 指定映射的文件,获取对应的解析器
        Digester digester = DigesterLoader.createDigester(DigesterLoader.class.getClassLoader().getResource("parse/rule.xml"));
        // 准备一个空对象,作为根标签映射的对象
        ClassBean classBean = new ClassBean();
        // 将该对象交给解析器
        digester.push(classBean);
        try {
            // 开始解析
            System.out.println("解析前:" + classBean);
            digester.parse(DigesterParse.class.getClassLoader().getResourceAsStream("students.xml"));
            System.out.println("解析后:" + classBean);
            System.out.println("学号为1的学生姓名:" + classBean.findStudent("1").getName());
            System.out.println("学号为2的学生姓名:" + classBean.findStudent("2").getName());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (SAXException e) {
            throw new RuntimeException(e);
        }
    }

}
